﻿using System;
using System.Globalization;
using System.Runtime.Serialization;
using System.Security;
using System.Security.Permissions;
using JetBrains.Annotations;
using OpenWaves.ImageTransformations;

namespace OpenWaves.Web.ImageTransformations
{
    [Serializable]
    public class ImageTransformationException : Exception
    {
        private readonly Url sourceImageUrl;
        private readonly string transformation;

        //
        // For guidelines regarding the creation of new exception types, see
        //    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/cpconerrorraisinghandlingguidelines.asp
        // and
        //    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dncscol/html/csharp07192001.asp
        //

        public ImageTransformationException()
        {
        }

        public ImageTransformationException(string message)
            : base(message)
        {
        }

        public ImageTransformationException(string message, Exception inner)
            : base(message, inner)
        {
        }

        public ImageTransformationException(string message, Url sourceImageUrl, IImageTransformation transformation)
            : this(message, sourceImageUrl, transformation, null)
        {
        }

        public ImageTransformationException(string message, Url sourceImageUrl, IImageTransformation transformation, Exception inner)
            : base(message, inner)
        {
            this.sourceImageUrl = sourceImageUrl;
            this.transformation = transformation.ToString();
        }

        protected ImageTransformationException(
            SerializationInfo info,
            StreamingContext context)
            : base(info, context)
        {
        }

        public Url SourceImageUrl
        {
            get { return this.sourceImageUrl; }
        }

        public string Transformation
        {
            get { return this.transformation; }
        }

        public override string Message
        {
            get
            {
                if (String.IsNullOrEmpty(base.Message) == false)
                    return base.Message;

                return String.Format(CultureInfo.InvariantCulture,
                    "Image could not be transformed. SourceImageUrl: {0}, Transformation: {1}",
                    this.SourceImageUrl,
                    this.Transformation);
            }
        }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")]
        [SecurityCritical]
        public override void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            base.GetObjectData(info, context);
            info.AddValue("sourceImageUrl", this.sourceImageUrl);
            info.AddValue("transformation", this.transformation);
        }
    }
}